import dict from '../core/dict';
import utils from '../core/utils';
import logger from '../core/logger';
import common from '@ohos.app.ability.common';
import relationalStore from '@ohos.data.relationalStore';

export default class serviceCache {
  private context: common.Context;
  private static _instance: serviceCache;

  private constructor(context: common.Context) {
    this.context = context;
    this.init();
  }

  public static get instance(): serviceCache {
    return this._instance;
  }

  public static createInstance(context: common.Context): serviceCache {
    if (this._instance == null)
      this._instance = new serviceCache(context);
    return this._instance;
  }

  private get rdbStore() {
    return globalThis.cardRdbStore;
  }

  private async init() {
    if (utils.isEmpty(this.rdbStore)) {
      await relationalStore.getRdbStore(this.context, {
        name: 'CardDatabase.db',
        securityLevel: relationalStore.SecurityLevel.S1,
        encrypt: false
      }).then((rdbStore: relationalStore.RdbStore) => {
        if (!utils.isEmpty(rdbStore)) {
          rdbStore.executeSql(dict.CARD_TABLE_FORM)
          globalThis.cardRdbStore = rdbStore;
        }
      });
    }
  }

  private clear() {
    let predicates: relationalStore.RdbPredicates = new relationalStore.RdbPredicates(dict.CARD_TABLE_NAME);
    this.rdbStore.delete(predicates);
  }

  public getListData(): Promise<any[]> {
    return new Promise((resolve) => {
      let array = [];
      try {

      } catch (e) {
        logger.error(e);
      }
      resolve(array);
    });
  }

  private toFormData(dataSource: any[]): Promise<object> {
    return new Promise((resolve) => {
      let formData = {};
      try {
        resolve(formData);
      } catch (e) {
        logger.error(e);
        resolve(formData);
      }
    });
  }

  public getFormData(dataSource: any[]) {
    return new Promise((resolve) => {
      this.toFormData(dataSource).then((res) => {
        resolve(res);
      });
    });
  }
}